package com.zys.service.impl;

import java.util.Map;
import java.util.Date;
import java.util.HashMap;

import com.zys.pojo.Role;
import com.zys.pojo.Merchant;
import com.zys.dao.MerchantDao;
import com.zys.entity.QueryInfo;
import com.zys.entity.PageResult;
import com.github.pagehelper.Page;
import com.zys.service.MerchantService;
import com.github.pagehelper.PageHelper;

import org.springframework.util.DigestUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class MerchantServiceImpl implements MerchantService {

  @Autowired
  private MerchantDao merchantDao;

  @Override
  public PageResult<Merchant> getMerchantList(QueryInfo queryInfo) {
    PageHelper.startPage(queryInfo.getCurrentPage(), queryInfo.getPageSize());
    Page<Merchant> result = merchantDao.findByCondition(queryInfo.getCondition());
    return new PageResult<>(result.getTotal(), result.getResult());
  }

  @Override
  public void add(Merchant merchant) {
    merchant.setRole(new Role(6));
    merchant.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
    merchant.setCreateTime(new Date().getTime());
    merchantDao.add(merchant);
  }

  @Override
  public void deleteById(Integer id) {
    merchantDao.rmRelation4GoodsAndMerchant(id);
    merchantDao.delete(id);
  }

  @Override
  public Merchant findMerchantById(Integer id) {
    Merchant merchant = merchantDao.findMerchantById(id);
    merchant.setRole(merchantDao.findRoleByMerchantId(id));
    return merchant;
  }

  @Override
  public void changeMerchantRole(Integer mcId, Integer roleId) {
    Map<String, Object> map = new HashMap<>();
    map.put("mcId", mcId);
    map.put("rid", roleId);
    merchantDao.changeMerchantRole(map);
  }

  @Override
  public void edit(Merchant merchant) {
    merchantDao.edit(merchant);
  }
}
